package main

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
	"unicode"
)

func main() {
	//遍历字符串
	str := "我爱go语言"
	for index, elem := range str {
		fmt.Printf("第%d个元素的值是%c , %s ,%v \n", index, elem, elem, elem)

	}

	//遍历字符串的字节
	for index, elem := range []byte(str) {
		fmt.Println("第", index, "个字节,字节值是", elem)
		fmt.Printf("%d:%x \n", index, elem)
	}

	//字符串中带有中文的使用 rune
	for index, elem := range []rune(str) {
		fmt.Printf("%d:%c ", index, elem)
	}

	fmt.Println()
	//判断字符串是否包含Unicode值
	containsWo := strings.ContainsRune(str, '我')
	fmt.Println("是否包含Unicode我  ", containsWo)
	containsWoAi := strings.Contains(str, "我爱")
	fmt.Println("是否包含Unicode我爱  ", containsWoAi)

	indexUnicode := strings.IndexFunc("test-我爱go语言", func(r rune) bool {
		return unicode.Is(unicode.Han, r)
	})
	fmt.Println("中文出现的第一个下标是  ", indexUnicode)
	indexNumber := strings.IndexFunc("t1est-123-我爱go语言", func(r rune) bool {
		return unicode.IsNumber(r)
	})
	fmt.Println("数字出现的第一个下标是  ", indexNumber)
	//文件名后缀
	fileName := "java.jar"
	afterFileName := strings.SplitAfter(fileName, ".")
	fmt.Printf("afterFileName %T   %v ,后缀是 %v  ", afterFileName, afterFileName, afterFileName[len(afterFileName)-1])
	fmt.Println()
	//测试函数
	afterFileName2 := getFileName(fileName)
	fmt.Println("afterFileName2 == ", afterFileName2)

	//修剪字符串
	trimWo := strings.Trim(str, "我")
	fmt.Println(str, trimWo)
	//字符串比较
	foldWoDe := strings.EqualFold("wode", "WODE")
	fmt.Println("foldWoDe == ", foldWoDe)

	repeatWen := strings.Repeat("?", 3)
	fmt.Println("repeatWen == ", repeatWen)

	joinWen := strings.Join([]string{"?", "?", "?"}, ",")
	fmt.Println("joinWen == ", joinWen)

	atoi789, err := strconv.Atoi("789")
	fmt.Println("atoi789 == ", atoi789, "  err == ", err)
	parseInt789, err := strconv.ParseInt("789", 10, 0)
	fmt.Println("parseInt789 == ", parseInt789, "  err ==  ", err)

	//解析字符串
	parseBool1, err := strconv.ParseBool("1")
	parseBool0, err := strconv.ParseBool("0")
	parseBoolTrue, err := strconv.ParseBool("true")
	parseBoolFalse, err := strconv.ParseBool("false")

	fmt.Println("parseBool1 == ", parseBool1, " parseBool0 == ", parseBool0, "  parseBoolTrue  ==  ", parseBoolTrue, "  parseBoolFalse == ", parseBoolFalse)

	// 数字转化为字符串
	itoa10 := strconv.Itoa(10)
	fmt.Printf("itoa10 ==  %T  %v  \n", itoa10, itoa10)

	//正则,书中这里有语法错误
	match1, err := regexp.Match("^\\d{6,15}$", []byte{'1', '2', '3', '4', '5', '6', '7', '8', '9'})
	fmt.Println("match1 == ", match1)
	matchString, err := regexp.MatchString("^\\d{6,15}$", "123456789")
	fmt.Println("matchString  == ", matchString)
}
func getFileName(fileName string) string {
	array := strings.SplitAfter(fileName, ".")
	return array[len(array)-1]
}
